package com.knowledgegraph.common;

import cn.hutool.core.collection.CollUtil;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

import java.util.List;

/**
 * 分页结果封装类
 * 
 * @author system
 * @since 2024-01-01
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageResult<T> {
    
    /**
     * 数据列表
     */
    private List<T> data;
    
    /**
     * 总记录数
     */
    private Long total;
    
    /**
     * 当前页码
     */
    private Integer current;
    
    /**
     * 每页大小
     */
    private Integer pageSize;
    
    /**
     * 总页数
     */
    private Integer totalPages;
    
    /**
     * 是否有下一页
     */
    private Boolean hasNext;
    
    /**
     * 是否有上一页
     */
    private Boolean hasPrevious;
    
    /**
     * 构造方法
     */
    public PageResult(List<T> data, Long total, Integer current, Integer pageSize) {
        this.data = data;
        this.total = total;
        this.current = current;
        this.pageSize = pageSize;
        
        // 计算总页数
        this.totalPages = (int) Math.ceil((double) total / pageSize);
        
        // 计算是否有下一页和上一页
        this.hasNext = current < totalPages;
        this.hasPrevious = current > 1;
    }
    
    /**
     * 创建空的分页结果
     */
    public static <T> PageResult<T> empty(Integer current, Integer pageSize) {
        return new PageResult<>(CollUtil.newArrayList(), 0L, current, pageSize);
    }
    
    /**
     * 创建分页结果
     */
    public static <T> PageResult<T> of(List<T> data, Long total, Integer current, Integer pageSize) {
        return new PageResult<>(data, total, current, pageSize);
    }
}